package com.cloudkinto.controller.detective;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.detective.DefectiveService;
import com.cloudkinto.service.detective.vo.StartPickReq;
import com.cloudkinto.service.detective.vo.pick.PickInfoRes;
import com.cloudkinto.service.detective.vo.shelf.SubmitShelfInitRes;
import com.cloudkinto.vo.defective.DefectiveDetailRes;
import com.cloudkinto.vo.defective.DefectivePageDto;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-12-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "次品处理Wms模块")
@RequestMapping("/api/admin/wms/Defective")
public class DefectiveWmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(DefectiveWmsController.class);

    private final DefectiveService service;


    @UserSessionRequired
    @PutMapping("/startPicking")
    @ApiOperation(value = "提交拣货", notes = "提交拣货")
    public SingleResult startPicking(@RequestBody @Validated StartPickReq req) {
        service.startPicking(req, getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success();
    }


    @UserSessionRequired
    @PutMapping("/submitPicking")
    @ApiOperation(value = "拣货", notes = "拣货")
    public SingleResult submitPicking(@RequestBody @Validated PickInfoRes req) {
        int i = service.submitPicking(req, getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(i);
    }

    @UserSessionRequired
    @PostMapping("/queryPickingInfo")
    @ApiOperation(value = "拣货初始化按钮", notes = "拣货初始化按钮")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PickInfoRes.class))
    public SingleResult queryPickingInfo(@RequestBody @Validated StartPickReq req) {
        PickInfoRes wms = service.queryPickingInfo(req.getId(), getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(wms);
    }


    @UserSessionRequired
    @GetMapping("/submitShelfInit")
    @ApiOperation(value = "翻新上架初始化按钮", notes = "翻新上架初始化按钮")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SubmitShelfInitRes.class))
    public SingleResult submitShelfInit(Long id) {
        SubmitShelfInitRes wms = service.submitShelfInit(id, getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(wms);
    }
    @UserSessionRequired
    @PutMapping("/submitShelf")
    @ApiOperation(value = "翻新上架", notes = "翻新上架")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PickInfoRes.class))
    public SingleResult submitShelf(@RequestBody @Validated SubmitShelfInitRes req) {
        int wms = service.submitShelf(req, getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(wms);
    }


    @UserSessionRequired
    @PutMapping("/submitOut")
    @ApiOperation(value = "确认销毁/确认出库", notes = "确认销毁/确认出库")
    public SingleResult submitOut(@RequestBody @Validated StartPickReq req) {
        service.submitOut(req, getCurrentUserId(), getWarehouseId(), getTenantId("wms"));
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = DefectiveDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("storageId", getWarehouseId());
        map.put("source", "wms");
        map.put("id", id);
        return SingleResult.success("查询成功",service.detail(map));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getTenantId("wms"), "wms");
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public SingleResult list(HttpServletRequest request){
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "stockKind", value = "处理库存类型 1FNSKU(暂时不做) 2 SKU"),
            @ApiImplicitParam(name = "companyIds", value = "客户", type = "arr"),
            @ApiImplicitParam(name = "handleMethods", value = "处理类型(多选)", type = "arr"),
            @ApiImplicitParam(name = "handleMethod", value = "处理类型(单选)"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time submit_time 等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "defectiveNo", value = "单号"),
            @ApiImplicitParam(name = "sku", value = "sku"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = DefectivePageDto.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    @UserSessionRequired
    @GetMapping("/statusStatistics")
    @ApiOperation(value = "状态数量统计", notes = "状态数量统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "stockKind", value = "处理库存类型 1FNSKU(暂时不做) 2 SKU"),
            @ApiImplicitParam(name = "companyIds", value = "客户", type = "arr"),
            @ApiImplicitParam(name = "handleMethods", value = "处理类型(多选)", type = "arr"),
            @ApiImplicitParam(name = "handleMethod", value = "处理类型(单选)"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time submit_time 等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "defectiveNo", value = "单号"),
            @ApiImplicitParam(name = "sku", value = "sku"),
    })
    public SingleResult statusStatistics(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success(service.statusStatistics(map));
    }


    public Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String stockKind = request.getParameter("stockKind");
        String companyIds = request.getParameter("companyIds");
        String handleMethods = request.getParameter("handleMethods");
        String handleMethod = request.getParameter("handleMethod");
        String dateColumn = request.getParameter("dateColumn");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String defectiveNo = request.getParameter("defectiveNo");
        String sku = request.getParameter("sku");
        String status = request.getParameter("status");
        map.put("stockKind", stockKind);
        if (companyIds != null) {
            map.put("companyIds", Arrays.asList(companyIds.split(",")));
        }
        if (handleMethods != null) {
            map.put("handleMethods", Arrays.asList(handleMethods.split(",")));
        }
        map.put("handleMethod", handleMethod);
        map.put("dateColumn", dateColumn);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("defectiveNo", defectiveNo);
        map.put("sku", sku);
        map.put("status", status);
        map.put("tenantId", getTenantId("wms"));
        map.put("source", "wms");
        map.put("storageId", getWarehouseId());
        return map;
    }

    @UserSessionRequired
    @PostMapping("/printPick")
    @ApiOperation(value = "打印", notes = "打印")
    public void printPick(@RequestBody @Validated StartPickReq req, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("source", "wms");
        map.put("storageId", getWarehouseId());
        map.put("id", req.getId());
        service.printPick(map, response);
    }


}

